# 检查Chrome浏览器版本并提供ChromeDriver下载指导
# -*- coding: utf-8 -*-

import platform
import subprocess
import re
import sys
import webbrowser


def get_chrome_version():
    """获取已安装的Chrome浏览器版本"""
    system = platform.system()
    chrome_version = None
    
    try:
        if system == "Windows":
            # 在Windows上使用wmic命令获取Chrome版本
            cmd = 'wmic datafile where name="C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe" get Version /value'
            result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
            
            # 从输出中提取版本号
            match = re.search(r'Version=(\d+\.\d+\.\d+\.\d+)', result.stdout)
            if match:
                chrome_version = match.group(1)
            else:
                # 尝试32位路径
                cmd = 'wmic datafile where name="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe" get Version /value'
                result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
                match = re.search(r'Version=(\d+\.\d+\.\d+\.\d+)', result.stdout)
                if match:
                    chrome_version = match.group(1)
        elif system == "Darwin":  # macOS
            cmd = '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version'
            result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
            match = re.search(r'Google Chrome (\d+\.\d+\.\d+\.\d+)', result.stdout)
            if match:
                chrome_version = match.group(1)
        elif system == "Linux":
            cmd = 'google-chrome --version'
            result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
            match = re.search(r'Google Chrome (\d+\.\d+\.\d+\.\d+)', result.stdout)
            if match:
                chrome_version = match.group(1)
            else:
                # 尝试chromium-browser命令
                cmd = 'chromium-browser --version'
                result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
                match = re.search(r'Chromium (\d+\.\d+\.\d+\.\d+)', result.stdout)
                if match:
                    chrome_version = match.group(1)
    except Exception as e:
        print(f"获取Chrome版本时出错: {e}")
    
    return chrome_version


def main():
    print("=== ChromeDriver版本检测和下载助手 ===")
    
    # 获取Chrome版本
    chrome_version = get_chrome_version()
    
    if chrome_version:
        print(f"已检测到Chrome浏览器版本: {chrome_version}")
        
        # 提取主要版本号
        major_version = chrome_version.split('.')[0]
        print(f"Chrome主要版本号: {major_version}")
        
        # 提供下载指导
        print("\n请下载与您的Chrome版本兼容的ChromeDriver:")
        print("1. 访问ChromeDriver下载页面: https://chromedriver.chromium.org/downloads")
        print(f"2. 找到与您的Chrome版本({chrome_version})匹配的ChromeDriver版本")
        
        # 根据系统类型提供不同的下载链接
        system = platform.system()
        if system == "Windows":
            print("3. 下载Windows版本的ChromeDriver")
        elif system == "Darwin":
            print("3. 下载macOS版本的ChromeDriver")
        elif system == "Linux":
            print("3. 下载Linux版本的ChromeDriver")
        
        print("4. 解压下载的文件")
        print("5. 将chromedriver可执行文件保存到一个固定位置")
        print("6. 设置环境变量CHROME_DRIVER_PATH指向ChromeDriver的完整路径")
        
        # 提供手动设置环境变量的指导
        if system == "Windows":
            print("\nWindows设置环境变量指南:")
            print("- 右键点击'此电脑' -> '属性' -> '高级系统设置' -> '环境变量'")
            print("- 在'系统变量'中点击'新建'")
            print("- 变量名: CHROME_DRIVER_PATH")
            print("- 变量值: ChromeDriver可执行文件的完整路径，如 C:\\path\\to\\chromedriver.exe")
        else:
            print("\nmacOS/Linux设置环境变量指南:")
            print("- 编辑 ~/.bashrc 或 ~/.zshrc 文件")
            print("- 添加以下行: export CHROME_DRIVER_PATH=/path/to/chromedriver")
            print("- 保存文件后运行: source ~/.bashrc 或 source ~/.zshrc")
        
        # 自动打开ChromeDriver下载页面
        try:
            print("\n正在打开ChromeDriver下载页面...")
            webbrowser.open('https://chromedriver.chromium.org/downloads')
        except:
            print("无法自动打开浏览器，请手动访问下载页面")
    else:
        print("未检测到Chrome浏览器或无法获取版本信息")
        print("请手动检查您的Chrome浏览器版本，然后下载兼容的ChromeDriver")
        print("ChromeDriver下载页面: https://chromedriver.chromium.org/downloads")

    print("\n=== 修复完成后运行以下命令测试 ===")
    print("python integrated_spider.py")


if __name__ == "__main__":
    main()